--
-- Author: shunguo.chen
-- Date: 2017-12-18 16:34:36
--
-- MountGetView  坐骑获取界面

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MountGetScene

local SOUL_NUM_STR = L("lua_code_text_466")

function ClassRef:init()
    display.setCascadeOpacityEnabled(self._root, true)
    self._ui.Button_ok:addClickEventListener(function()
        self:closeView()
    end)

    local con = self._ui["Panel/Panel_content"]
    self._tltText = con:getChildByName("tlt")

    self._listView = con:getChildByName("ListView")
    self._itemModel = self._listView:getItem(0)
    self._itemModel:retain()
    self._listView:removeAllItems()


    local panelBar = con:getChildByName("Panel_bar")
    self._bar = panelBar:getChildByName("LoadingBar")
    self._numText = panelBar:getChildByName("num")
    self._panelBar = panelBar
end

function ClassRef:remove()
    self._itemModel:release()
    self._itemModel = nil
end

function ClassRef:refresh(mount)
    self._ui.tlt:setString(mount:getName())

    -- resource
    local resoruce = mount:getResourceList()
    if #resoruce > 0 then
        for i,v in ipairs(resoruce) do
            local item = self._itemModel:clone()
            self._listView:pushBackCustomItem(item)
            item:getChildByName("name"):setString(v[2])
            item:setPressedActionEnabled(false) 
            item:setBright(v[1] ~= 0)
            item:setTouchEnabled(v[1] ~= 0)
            item:addClickEventListener(function()
                self:hide(function(render)
                    executeFunc(self._onOperate, "openFunc", {feature = v[1], param = v[3]})
                    executeFunc(self._onCloseView, self)
                end)
            end)
        end
    else
        self._listView:removeAllItems()
    end

    local canBeComposed = mount:canBeComposed()
    self._tltText:setString(canBeComposed and L("SoulResource_title") or L("noSoulResource_title"))
    self._panelBar:setVisible(canBeComposed)
    if canBeComposed then
        local haveNum, needNum = mount:getQualityCostNum()
        self._bar:setPercent(haveNum/needNum * 100)
        self._numText:setString(string.format(SOUL_NUM_STR, haveNum, needNum))
    end
end

return ClassRef